﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.BackgroundAudio;
using System.Diagnostics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Devices;

namespace WP7_player
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(Instance_PlayStateChanged);
            progressSlider.ValueChanged += progressSlider_ValueChanged;
            System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer(); dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds 
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();

            ListSongs();
        }

        private void ListSongs()
        {
            MediaLibrary lib = new MediaLibrary();
            var SongName = (from m in lib.Songs select m.Name).ToList();
            listBox.ItemsSource = SongName;
            //MediaPlayer.Play(lib.Songs[0]);
        }


        void progressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (FocusManager.GetFocusedElement() == sender)
                BackgroundAudioPlayer.Instance.Position = new TimeSpan(0, 0, (int)progressSlider.Value);
        }

        void dt_Tick(object sender, EventArgs e)
        {
            progressSlider.Value = BackgroundAudioPlayer.Instance.Position.TotalSeconds;
            Debug.WriteLine(BackgroundAudioPlayer.Instance.Position.TotalSeconds);
        }

        //обработка кнопок
        private void PrevButton_Click(object sender, RoutedEventArgs e)
        {
            BackgroundAudioPlayer.Instance.SkipPrevious();
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
            {
                BackgroundAudioPlayer.Instance.Pause();
            }
            else
            {
                BackgroundAudioPlayer.Instance.Play();
            }
        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            BackgroundAudioPlayer.Instance.SkipNext();
        }

        //конец обработки кнопок

        //Состояние трека
        void Instance_PlayStateChanged(object sender, EventArgs e)
        {
            switch (BackgroundAudioPlayer.Instance.PlayerState)
            {
                case PlayState.Playing:
                    PlayButton.Content = "Pause";
                    progressSlider.Maximum = BackgroundAudioPlayer.Instance.Track.Duration.TotalSeconds;
                    break;

                case PlayState.Paused:
                case PlayState.Stopped:
                    PlayButton.Content = "Play";
                    break;
            }

            if (null != BackgroundAudioPlayer.Instance.Track)
            {
                TrackTitle.Text = BackgroundAudioPlayer.Instance.Track.Title + " by " + BackgroundAudioPlayer.Instance.Track.Artist;
            }
        }

        //статус после восстановления аппа
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
            {
                PlayButton.Content = "Pause";
                TrackTitle.Text = BackgroundAudioPlayer.Instance.Track.Title + " by " + BackgroundAudioPlayer.Instance.Track.Artist;

            }
            else
            {
                PlayButton.Content = "Play";
                TrackTitle.Text = "";
            }
        }

    }
}